#include "obstacle.h"
#include <QPixmap>
#include <QDebug>

Obstacle::Obstacle(ObstacleType type):type(type) {
    QString imageResource;
    switch(type){
    case OBSTACLE_1:
        imageResource=":/images/obstacle1.png";
        break;
    case OBSTACLE_2:
        imageResource=":/images/obstacle2.png";
        break;
    case OBSTACLE_3:
        imageResource=":/images/obstacle3.png";
        break;
    }


    QPixmap pix(imageResource);
    if(pix.isNull()) {
        qDebug() << "Failed to load obstacle image!";
        pix = QPixmap(60, 60);
        pix.fill(Qt::red);
    }
    if(type==OBSTACLE_3){
        setPixmap(pix.scaled(300,300,Qt::KeepAspectRatio,Qt::SmoothTransformation));
    }
    else{
        setPixmap(pix.scaled(120,120,Qt::KeepAspectRatio,Qt::SmoothTransformation));
    }
    setShapeMode(QGraphicsPixmapItem::BoundingRectShape); // 碰撞检测模式
}

void Obstacle::move() {
    setX(x() + MOVE_SPEED);
}

bool Obstacle::isOutOfScreen() const {
    return x() + pixmap().width() < 0;
}
